//
//  PinViewModel.swift
//  XMYouPin
//
//  Created by 王宗成 on 2019/7/30.
//  Copyright © 2019 王宗成. All rights reserved.
//

import UIKit

class PinViewModel: NSObject {

    var delegate:UIViewController?
    
    lazy var model:PinModel = {
        
        return loadData() ?? nil
        }()!
    
    func loadData() -> PinModel? {
        
        let path = Bundle.main.path(forResource: "YouPinData", ofType: "json")
        // 2. 获取json文件里面的内容,NSData格式
        let jsonData=NSData(contentsOfFile: path!)
        // 3. 解析json内容
        
        do {
            
            let json = try JSONSerialization.jsonObject(with: jsonData! as Data, options: .allowFragments)
            
            if let result = json as? Dictionary<String, Any> {
                
                return PinModel.deserialize(from: result["data"] as? Dictionary)
                
            }
            else
            {
                return nil
            }
            
        } catch  {
            
            print("解析失败")
            return nil
        }
        
    }
}

extension PinViewModel:UITableViewDelegate,UITableViewDataSource
{
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return model.rags?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        
        let tmodel:PinRagModel = model.rags![section]
        
        if tmodel.title?.isEmpty ?? true {
            
            return 0.1
        }
        else
        {
            return 40.0;
        }
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let tmodel:PinRagModel = model.rags![section]
        
        if tmodel.title?.isEmpty ?? true {
            
            return UIView()
        }
        else
        {
            let tmodel:PinRagModel = model.rags![section]
            let view:WZC_TableViewHeaderFooterView = tableView.dequeueReusableHeaderFooterView(withIdentifier: WZC_TableViewHeaderFooterView.identifier()) as! WZC_TableViewHeaderFooterView
            view.commonBtn1.setTitle("\(tmodel.title ?? "")  ", for: .normal)
            view.commonBtn1.setImage(IMAGE("order_arrow"), for: .normal)
            view.commonBtn1.setTitleColor(UIColor.hexColor(hexadecimal: "#333333"), for: .normal)
            view.commonBtn1.semanticContentAttribute = .forceRightToLeft
            view.commonBtn1.titleLabel?.font = Font(14)
            view.commonBtn1.snp_remakeConstraints { (make) in
                
                make.center.equalToSuperview()
                
            }
            
            
            
            view.block = {
                (a: Any) -> Void in
                
                let vc:WZC_WebViewController = WZC_WebViewController()
                vc.urlString = tmodel.more
                
                if let fathVc = self.delegate
                {
                    
                    fathVc.navigationController?.pushViewController(vc, animated: true)
                }
                
            }
            return view;
        }
        
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        let tmodel:PinRagModel = model.rags![section]
        return tmodel.rags?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        let tmodel:PinRagModel = model.rags![indexPath.section]
        let ttmodel:PinRagModel = tmodel.rags![indexPath.row]
        return ttmodel.height
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let tmodel:PinRagModel = model.rags![indexPath.section]
        let ttmodel:PinRagModel = tmodel.rags![indexPath.row]
   
        if ttmodel.editorName == "pinwei_home_banner"
        {
            let cell:PinBannerTableViewCell = tableView.dequeueReusableCell(withIdentifier: PinBannerTableViewCell.identifier(), for: indexPath) as! PinBannerTableViewCell
            cell.model = ttmodel
            return cell
        }
        else if ttmodel.editorName == "pinwei_home_kingkong" {
            let cell:HomeTodayActivityTableViewCell = tableView.dequeueReusableCell(withIdentifier: HomeTodayActivityTableViewCell.identifier(), for: indexPath) as! HomeTodayActivityTableViewCell
            cell.pinModel = ttmodel
            return cell;
            
        }
        else if ttmodel.editorName == "pinwei_home_big_article" {
            let cell:PinPinWeiTableViewCell = tableView.dequeueReusableCell(withIdentifier: PinPinWeiTableViewCell.identifier(), for: indexPath) as! PinPinWeiTableViewCell
            cell.model = ttmodel
            return cell;
            
        }
        else if ttmodel.editorName == "pinwei_home_article_cover" {
            let cell:PinDujiaTableViewCell = tableView.dequeueReusableCell(withIdentifier: PinDujiaTableViewCell.identifier(), for: indexPath) as! PinDujiaTableViewCell
            cell.model = ttmodel
            return cell;
            
        }
        else
        {
            let cell:HotSellTableViewCell = tableView.dequeueReusableCell(withIdentifier: HotSellTableViewCell.identifier(), for: indexPath) as! HotSellTableViewCell
     
            return cell;
        }
    }
}
